﻿using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infratructure
{
    public class Repository <T>:IRepository<T> where T:class
    {
        MyDBcontext DB;

        public Repository(MyDBcontext dB)
        {
            DB = dB;
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t">参数</param>
        /// <returns>影响行数</returns>
        public async Task<int> Addway(T t)
        {
            try
            {
                DB.Set<T>().Add(t);
                return await DB.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t">参数</param>
        /// <returns>影响行数</returns>
        public async Task<int> Updateway(T t)
        {
            try
            {
                DB.Set<T>().Update(t);
                return await DB.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="Id">主键</param>
        /// <returns>数据</returns>
       public  async Task<T> GetValue(long Id)
        {
            try
            {
                return await DB.Set<T>().FindAsync(Id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询数据集合
        /// </summary>
        /// <returns>数据集合</returns>
        public IQueryable<T> GetAll()
        {
            try
            {
                return DB.Set<T>();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
